package com.zhouyibike.uu.ui.activity.mybike.Twentyoffer;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetBicycleInterfaceResult;
import com.zhouyibike.uu.entity.TwentyGetBicycleResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.share.MyShareMsgActivity3;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.Util;

import java.util.HashMap;
import java.util.Map;

public class Buy20ShareActivity extends BaseActivity implements View.OnClickListener{
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private TextView golingqu,yifenxiangrenshu;
    private Button btn_liyc,btn_ckye;
    private ImageView titleiv,back1,quyaoqing;
    private RelativeLayout huodongneirong,huodongjiazai;
    private boolean haslingqu=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy20share);
        initView();
        getMsg();
        initListener();
    }

    private void initListener() {
        quyaoqing.setOnClickListener(this);
        mBtnBack.setOnClickListener(this);
        golingqu.setOnClickListener(this);
    }

    private void initView() {
        huodongneirong= (RelativeLayout) findViewById(R.id.huodongneirong);
        huodongjiazai= (RelativeLayout) findViewById(R.id.huodongjiazai);
        back1= (ImageView) findViewById(R.id.back1);
        golingqu= (TextView) findViewById(R.id.golingqu);
        quyaoqing= (ImageView) findViewById(R.id.quyaoqing);
        yifenxiangrenshu= (TextView) findViewById(R.id.yiyaoqingrenshu);
//        quyaoqing.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //UNDERLINE_TEXT_FLAG下划线删除线STRIKE_THRU_TEXT_FLAG
//        quyaoqing.getPaint().setAntiAlias(true);//抗锯齿
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        titleiv= (ImageView) findViewById(R.id.titleiv);
//        neirong= (TextView) findViewById(R.id.neirong);
//        neirong.setText(Html.fromHtml(str));
        mBtnTitle.setVisibility(View.GONE);
        titleiv= (ImageView) findViewById(R.id.titleiv);
        titleiv.setVisibility(View.VISIBLE);
        golingqu.setEnabled(false);
//        titleiv.setBackground(getResources().getDrawable(R.drawable.ldc));
    }
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        showProgressDialog();
        addSubscription(apiStores.GetBicycleInterface(params),
                new ApiCallback<GetBicycleInterfaceResult>() {

                    @Override
                    public void onSuccess(GetBicycleInterfaceResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            setdata(model);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    @TargetApi(26)
    private void setdata(GetBicycleInterfaceResult info) {
        Picasso.with(mActivity).load(info.getData().getTitleImg().getImagePath()) .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE).into(titleiv);
        Picasso.with(mActivity).load(info.getData().getQuYaoImage().getImagePath()) .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE).into(quyaoqing);
        Util.setRtitle(quyaoqing,Double.parseDouble(info.getData().getQuYaoImage().getWidth()),Double.parseDouble(info.getData().getQuYaoImage().getHeight()),Util.dp2px(this,35));
        Util.setRtitle(titleiv,Double.parseDouble(info.getData().getTitleImg().getWidth()),Double.parseDouble(info.getData().getTitleImg().getHeight()),Util.dp2px(this,22));
        Picasso.with(mActivity).load(info.getData().getBgImg().getImagePath()) .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE).into(back1, new Callback() {
            @Override
            public void onSuccess() {
                huodongneirong.setVisibility(View.VISIBLE);
                huodongjiazai.setVisibility(View.GONE);
            }
            @Override
            public void onError() {

            }
        });
        yifenxiangrenshu.setText(info.getData().getInvitedCount()+"");
        if (info.getData().isCanGet())
        {
            golingqu.setEnabled(true);
            golingqu.setTextColor(Color.WHITE);
            golingqu.setBackground(getResources().getDrawable(R.drawable.lingdanche_pressed));
        }else{
//            golingqu.setEnabled(false);
//            golingqu.setTextColor(Color.rgb(184,184,184));
//            golingqu.setBackground(getResources().getDrawable(R.drawable.lingdanche_notpressed));
            golingqu.setEnabled(true);
            golingqu.setTextColor(Color.WHITE);
            golingqu.setBackground(getResources().getDrawable(R.drawable.lingdanche_pressed));
        }
        if (info.getData().isIsGeted())
        {
            golingqu.setEnabled(false);
            golingqu.setText("已领取");
            golingqu.setTextColor(Color.argb(150,255,255,255));
            golingqu.getBackground().setAlpha(100);
//            golingqu.setBackground(getResources().getDrawable(R.drawable.lingdanche_notpressed));
        }
    }
    private void getMsg2(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
//        showProgressDialog();
        addSubscription(apiStores.TwentyGetBicycle(params),
                new ApiCallback<TwentyGetBicycleResult>() {

                    @Override
                    public void onSuccess(TwentyGetBicycleResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            toastShow(model.getMessage());
                            golingqu.setEnabled(false);
                            golingqu.setText("已领取");
                            golingqu.setTextColor(Color.argb(150,255,255,255));
                            golingqu.getBackground().setAlpha(100);
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
//                        dismissProgressDialog();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.quyaoqing:
                Intent intent1=new Intent(this, MyShareMsgActivity3.class);
                startActivityForResult(intent1,500);
                break;
            case R.id.golingqu:
                getMsg2();
                break;
        }
    }
}
